package com.bhs.sansong.ui.discountCoupon;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.LogisticsDiscountCouponAdapter;
import com.bhs.sansong.adapter.PlatformAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.DiscountCoupon;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 领券中心
 */
public class CouponCollectionCenterActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private LinearLayout mLlSearch;
    private TextView mEtSearch;
    private RecyclerView mRvPlatform;
    private PlatformAdapter platformAdapter;
    private RecyclerView mRvSpecialRailwayLine;
    private LogisticsDiscountCouponAdapter discountCouponAdapter;

    private int page = 1;
    private TextView mTvPlatform;
    private TextView mTvSpecialRailwayLine;
    private FrameLayout mFlScrollBar;
    private View mLine;

    @Override
    public int initLayout() {
        return R.layout.activity_coupon_collection_center;
    }

    @Override
    public void initView() {
        mLlSearch = findViewById(R.id.ll_search);
        mEtSearch = findViewById(R.id.et_search);
        mRvPlatform = findViewById(R.id.rv_platform);
        mRvSpecialRailwayLine = findViewById(R.id.rv_special_railway_line);
        networkRequest(netApi.couponList("100", page), this);
        mTvPlatform = findViewById(R.id.tv_platform);
        mTvSpecialRailwayLine = findViewById(R.id.tv_special_railway_line);
        mFlScrollBar = findViewById(R.id.fl_scroll_bar);
        mLine = findViewById(R.id.line);
        mEtSearch.setOnClickListener(this);
    }


    @Override
    public void initData() {
        platformAdapter = new PlatformAdapter();
        mRvPlatform.setAdapter(platformAdapter);
        mRvPlatform.setLayoutManager(new LinearLayoutManager(mActivity, RecyclerView.HORIZONTAL, false));
        discountCouponAdapter = new LogisticsDiscountCouponAdapter();
        mRvSpecialRailwayLine.setAdapter(discountCouponAdapter);
        mRvSpecialRailwayLine.setLayoutManager(new LinearLayoutManager(mActivity));
        setScrollBar();
        platformAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (platformAdapter.getData().get(position).getCan_take() == 1) {
                    networkRequest(netApi.takeCoupon(platformAdapter.getData().get(position).getId()), CouponCollectionCenterActivity.this, 555);
                    platformAdapter.getData().get(position).setCan_take(0);
                    platformAdapter.notifyItemChanged(position);
                }
            }
        });
        discountCouponAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId()==R.id.tv_receive_coupon){
                    networkRequest(netApi.takeCoupon(discountCouponAdapter.getData().get(position).getId()), CouponCollectionCenterActivity.this, 555);
                    discountCouponAdapter.getData().get(position).setCan_take(0);
                    discountCouponAdapter.notifyItemChanged(position);
                }
            }
        });

    }

    float endX = 0;

    //设置滚动条
    private void setScrollBar() {
        //这里的mRvHx是需要绑定滚动条的RecyclerView
        mRvPlatform.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                //整体的总宽度，注意是整体，包括在显示区域之外的。
                int range = mRvPlatform.computeHorizontalScrollRange();
                float density = getScreenDensity();
                //计算出溢出部分的宽度，即屏幕外剩下的宽度
                float maxEndX = range + (10 * density) + 5 - MyUtils.getScreenWidth(mActivity);
                //滑动的距离
                endX += dx;
                //计算比例
                float proportion = endX / maxEndX;

                //计算滚动条宽度
                int transMaxRange = ((ViewGroup) mLine.getParent()).getWidth() - mLine.getWidth();
                //设置滚动条移动
                mLine.setTranslationX(transMaxRange * proportion);
            }

        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            DiscountCoupon bean = new Gson().fromJson(json, DiscountCoupon.class);
            if (bean.getCode().equals("1")) {
                if (bean.getData().getPlat().getList().size() > 0) {
                    platformAdapter.setNewData(bean.getData().getPlat().getList());
                } else {
                    mTvPlatform.setVisibility(View.GONE);
                    mFlScrollBar.setVisibility(View.GONE);
                }

                if (bean.getData().getCompany().getList().size() > 0) {
                    discountCouponAdapter.setNewData(bean.getData().getCompany().getList());
                } else {
                    mTvSpecialRailwayLine.setVisibility(View.GONE);
                }
            }
        } else if (requestCode == 555) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.getCode().equals("1")) {
                ToastUtil.show("领取成功");
            }else {
                networkRequest(netApi.couponList("100", page), this);
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    public float getScreenDensity() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
        }
        return dm.density;
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(mActivity,CouponSearchActivity.class));
    }
}